#include <iostream>
#include <time.h>
#include <string>
#include "Microwave.h"
#include "Clock.h"
#include "Magnetron.h"
#include "Motor.h"
#include "Door.h"
#include "Display.h"
#include "LightBulb.h"
#include "Keyboard.h"

using namespace std;
using namespace Green;

Microwave::Microwave()
{
	ck = new Clock();
	mag = new Magnetron(false);
	d = new Door(false, true);
	mt = new Motor(false);
	dis = new Display();
	light = new LightBulb();
	key = new Keyboard();

	cookData c;
	
	c.l = MEDIUM;
	c.d = 60;
	preset[PIZZA] = c;

	c.l = HIGH;
	c.d = 120;
	preset[DEFROST] = c;

	c.l = LOW;
	c.d = 30;
	preset[POPTART] = c;

	c.l = MEDIUM;
	c.d = 180;
	preset[POPCORN] = c;
}

Microwave::~ Microwave()
{
	delete ck;
	delete mag;
	delete d;
	delete mt;
	delete dis;
	delete light;
	delete key;
}

void Microwave::run()
{
	int in=5;
	FoodType input;
	
		
	cout << "Welcome to Microwave" << endl;
	cout << endl;
	
		dis->displayTimeOfDay(ck);
	//do
	while(in < 0 || in >= 5)
	{	
		dis->displayKeyboard(key);

		in = key->getKeyboardInput();
	
		if(in == 1)
		{
			input = PIZZA;
		}
		else if (in == 2)
		{
			input = DEFROST;
		}
		else if (in == 3)
		{
			input = POPTART;
		}
		else if (in == 4)
		{
			input = POPCORN;
		}
		else
		{
			input = NOTHING;
		}
	
		switch (input)
		{
			case PIZZA:
				cookMyFood();
				ck->CountDown( (preset[(Green::FoodType)in]).d );
				doneCooking();
				break;
			case DEFROST:
				cookMyFood();
				ck->CountDown( (preset[(Green::FoodType)in]).d );
				doneCooking();
				break;
			case POPTART:
				cookMyFood();
				ck->CountDown( (preset[(Green::FoodType)in]).d );
				doneCooking();
				break;
			case POPCORN:
				cookMyFood();
				ck->CountDown( (preset[(Green::FoodType)in]).d );
				doneCooking();
				break;
			default:
				cout << "Not a vaild option" << endl;
				break;
		}
	}//while(in < 0 && in >= 5);

}

void Microwave::cookMyFood()
{
	d->setDoorState(true, false);
	if(d->getDoorStateOpenOrClosed() == false)
	{
		cout << "Door is closed ";

		if(d->getDoorStateLocked() == true)
		{
			cout << "Door is locked" << endl;
		}
	}
	
	mt->setMotorState(true);
	if(mt->getMotorState() == true)
	{
		cout << "Motor is on" << endl;
	}

	mag->setMagnetronLevel(true);
	if(mag->getMagnetronLevel() == true)
	{ 
		cout << "Magnetron is on" << endl;
	}

	light->setLightStateOn();
	if(light->isOn() == true)
	{
		cout << "Light is on" << endl;
	}
}

void Microwave::doneCooking()
{	
	mt->setMotorState(false);
	if(mt->getMotorState() == false)
	{
		cout << "Motor is off" << endl;
	}

	mag->setMagnetronLevel(false);
	if(mag->getMagnetronLevel() == false)
	{ 
		cout << "Magnetron is off" << endl;
	}

	light->setLightStateOff();
	if(light->isOn() == false)
	{
		cout << "Light is off" << endl;
	}
	
	d->setDoorState(false, true);
	if(d->getDoorStateLocked() == false)
	{
		cout << "Door is unlocked ";

		if(d->getDoorStateOpenOrClosed() == true)
		{
			cout << "Door can be opened" << endl;
		}
	}
}

